import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:openim/pages/contacts/add_by_search/add_by_search_logic.dart';
import 'package:openim/routes/app_navigator.dart';
import 'package:openim_common/openim_common.dart';
import 'package:sprintf/sprintf.dart';

import 'group_setup_logic.dart';

class GroupSetupPage extends StatelessWidget {
  final logic = Get.find<GroupSetupLogic>();

  GroupSetupPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TitleBar.back(title: StrRes.groupChatSetup, centerTitle: true),
      backgroundColor: Styles.c_F8F9FA,
      body: Obx(() => SingleChildScrollView(
            child: Column(
              children: [
                // 群头像和群简介区域（新增）
                if (logic.isJoinedGroup.value) _buildGroupHeaderSection(),
                10.verticalSpace,
                if (logic.isJoinedGroup.value) _buildMemberView(),
                10.verticalSpace,
                // 群聊信息设置区域
                if (logic.isJoinedGroup.value) _buildGroupInfoSection(),
                10.verticalSpace,
                // 聊天设置区域
                if (logic.isJoinedGroup.value) _buildChatSettingsSection(),
                10.verticalSpace,
                // 群管理（仅群主可见）
                // if (logic.isOwner)
                //   _buildItemView(
                //     text: StrRes.groupManage,
                //     showRightArrow: true,
                //     isTopRadius: true,
                //     isBottomRadius: true,
                //     onTap: logic.groupManage,
                //   ),
                if (logic.isOwner) 10.verticalSpace,
                // 退出/解散群聊
                if (!logic.isOwner)
                  _buildItemView(
                    text: logic.isJoinedGroup.value ? StrRes.exitGroup : StrRes.delete,
                    textStyle: Styles.ts_FF381F_17sp,
                    showRightArrow: false,
                    isTopRadius: true,
                    isBottomRadius: true,
                    onTap: logic.quitGroup,
                  ),
                if (logic.isOwner)
                  _buildItemView(
                    text: StrRes.dismissGroup,
                    textStyle: Styles.ts_FF381F_17sp,
                    isTopRadius: true,
                    isBottomRadius: true,
                    showRightArrow: false,
                    onTap: logic.quitGroup,
                  ),
                40.verticalSpace,
              ],
            ),
          )),
    );
  }
  
  // 群头像和群简介区域（新增）
  Widget _buildGroupHeaderSection() => Container(
    margin: EdgeInsets.symmetric(horizontal: 10.w),
    padding: EdgeInsets.all(20.w),
    decoration: BoxDecoration(
      color: Styles.c_FFFFFF,
      borderRadius: BorderRadius.circular(6.r),
    ),
    child: Column(
      children: [
        // 群头像
        GestureDetector(
          onTap: logic.isOwnerOrAdmin ? logic.modifyGroupAvatar : null,
          child: Stack(
            children: [
              AvatarView(
                width: 80.w,
                height: 80.h,
                url: logic.groupInfo.value.faceURL,
                file: logic.avatar.value,
                text: logic.groupInfo.value.groupName,
                textStyle: Styles.ts_FFFFFF_16sp,
                isGroup: true,
              ),
              if (logic.isOwnerOrAdmin)
                Positioned(
                  bottom: 0,
                  right: 0,
                  child: Container(
                    width: 24.w,
                    height: 24.h,
                    decoration: BoxDecoration(
                      color: Styles.c_0089FF,
                      borderRadius: BorderRadius.circular(12.r),
                      border: Border.all(color: Styles.c_FFFFFF, width: 2.w),
                    ),
                    child: Icon(
                      Icons.camera_alt,
                      size: 12.w,
                      color: Styles.c_FFFFFF,
                    ),
                  ),
                ),
            ],
          ),
        ),
        16.verticalSpace,
        
        // 群名称
        GestureDetector(
          onTap: logic.isOwnerOrAdmin ? () => logic.modifyGroupName(logic.conversationInfo.value.faceURL) : null,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Flexible(
                child: (logic.groupInfo.value.groupName ?? '').toText
                  ..style = Styles.ts_0C1C33_20sp_medium
                  ..textAlign = TextAlign.center
                  ..maxLines = 2
                  ..overflow = TextOverflow.ellipsis,
              ),
              if (logic.isOwnerOrAdmin) ...[
                4.horizontalSpace,
                Icon(
                  Icons.edit,
                  size: 16.w,
                  color: Styles.c_8E9AB0,
                ),
              ],
            ],
          ),
        ),
        
        8.verticalSpace,
        
        // 群ID和成员数
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            GestureDetector(
              onTap: logic.copyGroupID,
              child: Row(
                children: [
                  'ID: ${logic.groupInfo.value.groupID}'.toText
                    ..style = Styles.ts_8E9AB0_14sp,
                  4.horizontalSpace,
                  Icon(
                    Icons.copy,
                    size: 12.w,
                    color: Styles.c_8E9AB0,
                  ),
                ],
              ),
            ),
            16.horizontalSpace,
            // 第167行附近 - 群基本信息区域的人数显示
            Obx(() => logic.showGroupMemberCount.value 
              ? ('${logic.groupInfo.value.memberCount ?? 0}人'.toText
                  ..style = Styles.ts_8E9AB0_14sp)
              : ''.toText),
          ],
        ),
        
        16.verticalSpace,
        
        // 群简介
        Container(
          width: double.infinity,
          padding: EdgeInsets.all(12.w),
          decoration: BoxDecoration(
            color: Styles.c_F8F9FA,
            borderRadius: BorderRadius.circular(8.r),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  '群简介'.toText
                    ..style = Styles.ts_0C1C33_14sp_medium,
                  if (logic.isOwnerOrAdmin) ...[
                    Spacer(),
                    GestureDetector(
                      onTap: () => logic.editGroupIntroduction(),
                      child: Icon(
                        Icons.edit,
                        size: 14.w,
                        color: Styles.c_8E9AB0,
                      ),
                    ),
                  ],
                ],
              ),
              8.verticalSpace,
              (logic.groupInfo.value.introduction?.isNotEmpty == true 
                  ? logic.groupInfo.value.introduction!
                  : '暂无群简介').toText
                ..style = logic.groupInfo.value.introduction?.isNotEmpty == true 
                    ? Styles.ts_0C1C33_14sp 
                    : Styles.ts_8E9AB0_14sp
                ..maxLines = 3
                ..overflow = TextOverflow.ellipsis,
            ],
          ),
        ),
      ],
    ),
  );
  Widget _buildPublicLinkSection() => Container(
    height: 80.h,
    margin: EdgeInsets.symmetric(horizontal: 10.w),
    padding: EdgeInsets.symmetric(horizontal: 16.w),
    decoration: BoxDecoration(
      color: Styles.c_FFFFFF,
      borderRadius: BorderRadius.only(
        topLeft: Radius.circular(6.r),
        topRight: Radius.circular(6.r),
      ),
    ),
    child: Row(
      children: [
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              '公共链接'.toText
                ..style = Styles.ts_0C1C33_17sp,
              Obx(() => logic.getGroupInviteLinkDisplay().toText
                ..style = TextStyle(
                  color: Styles.c_FFB300,
                  fontSize: 12.sp,
                )
                ..maxLines = 1
                ..overflow = TextOverflow.ellipsis),
            ],
          ),
        ),
        
        12.horizontalSpace, 
        
        // 右侧按钮组
        Row(
          children: [
            // 添加好友按钮（收费开启时隐藏）
            Visibility(
              visible: !logic.isGroupFeeEnabled,
              child: GestureDetector(
                onTap: () {
                  // TODO: 添加好友逻辑
                  logic.addMember();
                },
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Container(
                      width: 32.w,
                      height: 32.h,
                      decoration: BoxDecoration(
                        color: Styles.c_FFB300_opacity50,
                        borderRadius: BorderRadius.circular(6.r),
                      ),
                      child: Icon(
                        Icons.person_add,
                        size: 20.w,
                        color: Styles.c_FFB300,
                      ),
                    ),
                    2.verticalSpace,
                    '添加好友'.toText
                      ..style = TextStyle(
                        color: Styles.c_8E9AB0,
                        fontSize: 10.sp,
                      ),
                  ],
                ),
              ),
            ),
            
            12.horizontalSpace,
            
            // 复制链接按钮
            GestureDetector(
              onTap: () {
                // TODO: 复制链接逻辑
                logic.copyGroupInviteLink();
              },
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    width: 32.w,
                    height: 32.h,
                    decoration: BoxDecoration(
                      color: Styles.c_0089FF_opacity10,
                      borderRadius: BorderRadius.circular(6.r),
                    ),
                    child: Icon(
                      Icons.link,
                      size: 20.w,
                      color: Styles.c_0089FF,
                    ),
                  ),
                  2.verticalSpace,
                  '复制链接'.toText
                    ..style = TextStyle(
                      color: Styles.c_8E9AB0,
                      fontSize: 10.sp,
                    ),
                ],
              ),
            ),
            
            12.horizontalSpace,
            
            // 群二维码按钮
            GestureDetector(
              onTap: () {
                // TODO: 群二维码逻辑
                logic.viewGroupQrcode();
              },
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    width: 32.w,
                    height: 32.h,
                    decoration: BoxDecoration(
                      color: const Color(0xFFE8F5E8),
                      borderRadius: BorderRadius.circular(6.r),
                    ),
                    child: Icon(
                      Icons.qr_code,
                      size: 20.w,
                      color: const Color(0xFF4CAF50),
                    ),
                  ),
                  2.verticalSpace,
                  '群二维码'.toText
                    ..style = TextStyle(
                      color: Styles.c_8E9AB0,
                      fontSize: 10.sp,
                    ),
                ],
              ),
            ),
          ],
        ),
      ],
    ),
  );
    
  // 群聊信息设置区域
  Widget _buildGroupInfoSection() => Column(
    children: [
      // 公共链接区域
      _buildPublicLinkSection(),
      Container(
        height: 1.h,
        color: Styles.c_E8EAEF,
        margin: EdgeInsets.only(left: 26.w),
      ),
      _buildItemView(
        text: '群聊名称',
        value: logic.groupInfo.value.groupName ?? '',
        showRightArrow: logic.isOwnerOrAdmin,
        isTopRadius: true,
        onTap: logic.isOwnerOrAdmin ? () => logic.modifyGroupName(logic.conversationInfo.value.faceURL) : null,
      ),
      Container(
        height: 1.h,
        color: Styles.c_E8EAEF,
        margin: EdgeInsets.only(left: 26.w),
      ),
      _buildItemView(
        text: '群头像',
        showRightArrow: logic.isOwnerOrAdmin,
        onTap: logic.isOwnerOrAdmin ? logic.modifyGroupAvatar : null,
        customRight: Container(
          margin: EdgeInsets.only(right: logic.isOwnerOrAdmin ? 8.w : 0),
          child: AvatarView(
            width: 32.w,
            height: 32.h,
            url: logic.groupInfo.value.faceURL,
            file: logic.avatar.value,
            text: logic.groupInfo.value.groupName,
            textStyle: Styles.ts_FFFFFF_12sp,
            isGroup: true,
          ),
        ),
      ),
      Container(
        height: 1.h,
        color: Styles.c_E8EAEF,
        margin: EdgeInsets.only(left: 26.w),
      ),
      _buildItemView(
        text: '群公告',
        value: logic.groupInfo.value.notification?.isNotEmpty == true 
            ? logic.groupInfo.value.notification!.length > 20 
                ? '${logic.groupInfo.value.notification!.substring(0, 20)}...'
                : logic.groupInfo.value.notification!
            : '全新积分兑换活动来啦！亲爱的宝宝们，为感谢大家一路以来的支持，我们特别推出《积分兑换大回馈》活动，无论你是新朋友还是老朋友，只要看在本活消费...',
        showRightArrow: true,
        isBottomRadius: true,
        onTap: () => logic.viewGroupAnnouncement(),
      ),
      // 如果是群主，在群公告下方插入“群管理”
      if (logic.isOwner) ...[
        Container(
          height: 1.h,
          color: Styles.c_E8EAEF,
          margin: EdgeInsets.only(left: 26.w),
        ),
        _buildItemView(
          text: StrRes.groupManage,
          showRightArrow: true,
          isBottomRadius: true,
          onTap: logic.groupManage,
        ),
      ],
    ],
  );

  // 聊天设置区域
  Widget _buildChatSettingsSection() => Column(
    children: [
        // 只有管理员才显示"显示所有群成员"选项
      if (logic.isOwnerOrAdmin) ...[
        _buildItemView(
          text: StrRes.showAllGroupMembers,
          showSwitchButton: true,
          switchOn: logic.showAllGroupMembers.value,
          isTopRadius: true,
          onChanged: (value) => logic.toggleShowAllGroupMembers(value),
        ),
        Container(
          height: 1.h,
          color: Styles.c_E8EAEF,
          margin: EdgeInsets.only(left: 26.w),
        ),
        _buildItemView(
          text: StrRes.showGroupMemberCount,
          showSwitchButton: true,
          switchOn: logic.showGroupMemberCount.value,
          onChanged: (value) => logic.toggleShowGroupMemberCount(value),
        ),
        Container(
          height: 1.h,
          color: Styles.c_E8EAEF,
          margin: EdgeInsets.only(left: 26.w),
        ),
         // 新增：群禁言设置（仅管理员可见）
        _buildItemView(
          text: StrRes.groupMuted,
          showSwitchButton: true,
          switchOn: logic.isGroupMuted,
          onChanged: (value) => logic.toggleGroupMute(),
        ),
        Container(
          height: 1.h,
          color: Styles.c_E8EAEF,
          margin: EdgeInsets.only(left: 26.w),
        ),
        // 新增：群成员互加好友设置（仅管理员可见）
        _buildItemView(
          text: StrRes.allowMemberAddFriend,
          showSwitchButton: true,
          switchOn: logic.allowMemberAddFriend,
          onChanged: (value) => logic.toggleAllowMemberAddFriend(),
        ),
        Container(
          height: 1.h,
          color: Styles.c_E8EAEF,
          margin: EdgeInsets.only(left: 26.w),
        ),
         _buildItemView(
        text: '群收费设置',
        customRight: Obx(() => Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (logic.isGroupFeeEnabled)
              Text(
                '\$${logic.groupFee.value.toStringAsFixed(2)}',
                style: TextStyle(
                  fontSize: 14.sp,
                  color: Styles.c_0089FF,
                  fontWeight: FontWeight.w500,
                ),
              ),
            8.horizontalSpace,
            CupertinoSwitch(
              value: logic.isGroupFeeEnabled,
              onChanged: (value) => logic.toggleGroupFee(),
              activeColor: Styles.c_FFB300,
            ),
          ],
        )),
        isTopRadius: false,
        onTap: () => logic.toggleGroupFee(),
      ),
      Container(
        height: 1.h,
        color: Styles.c_E8EAEF,
        margin: EdgeInsets.only(left: 26.w),
      ),
      ],
      _buildItemView(
        text: '消息免打扰',
        showSwitchButton: true,
        switchOn: logic.isNotDisturb,
        isTopRadius: logic.isOwnerOrAdmin ? false : true, // 根据是否有管理员设置调整圆角
        onChanged: (value) => logic.toggleNotDisturb(),
      ),
      Container(
        height: 1.h,
        color: Styles.c_E8EAEF,
        margin: EdgeInsets.only(left: 26.w),
      ),
      _buildItemView(
        text: '置顶聊天',
        showSwitchButton: true,
        switchOn: logic.conversationInfo.value.isPinned ?? false,
        onChanged: (value) => logic.toggleTopConversation(),
      ),
      Container(
        height: 1.h,
        color: Styles.c_E8EAEF,
        margin: EdgeInsets.only(left: 26.w),
      ),
      _buildItemView(
        text: '查找聊天记录',
        showRightArrow: true,
        onTap: () => logic.searchChatHistory(),
      ),
      Container(
        height: 1.h,
        color: Styles.c_E8EAEF,
        margin: EdgeInsets.only(left: 26.w),
      ),
      _buildItemView(
        text: '清空聊天记录',
        showRightArrow: false,
        isBottomRadius: true,
        onTap: () => logic.clearChatHistory(),
      ),
    ],
  );

  Widget _buildBaseInfoView() => Container(
        height: 80.h,
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        margin: EdgeInsets.symmetric(horizontal: 10.w, vertical: 10.h),
        decoration: BoxDecoration(
          color: Styles.c_FFFFFF,
          borderRadius: BorderRadius.circular(6.r),
        ),
        child: Row(
          children: [
            SizedBox(
              width: 50.h,
              height: 50.h,
              child: Stack(
                children: [
                  AvatarView(
                    width: 48.w,
                    height: 48.h,
                    url: logic.groupInfo.value.faceURL,
                    file: logic.avatar.value,
                    text: logic.groupInfo.value.groupName,
                    textStyle: Styles.ts_FFFFFF_14sp,
                    isGroup: true,
                    onTap: logic.isOwnerOrAdmin ? logic.modifyGroupAvatar : null,
                  ),
                  if (logic.isOwnerOrAdmin)
                    Align(
                        alignment: Alignment.bottomRight,
                        child: ImageRes.editAvatar.toImage
                          ..width = 14.w
                          ..height = 14.h)
                ],
              ),
            ),
            10.horizontalSpace,
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap:
                        logic.isOwnerOrAdmin ? () => logic.modifyGroupName(logic.conversationInfo.value.faceURL) : null,
                    child: Row(
                      children: [
                        ConstrainedBox(
                            constraints: BoxConstraints(maxWidth: 200.w),
                            child: (logic.groupInfo.value.groupName ?? '').toText..style = Styles.ts_0C1C33_17sp),
                        Obx(() => logic.showGroupMemberCount.value 
                          ? ('${logic.groupInfo.value.memberCount ?? 0}人'.toText
                              ..style = Styles.ts_8E9AB0_14sp)
                          : ''.toText),
                        6.horizontalSpace,
                        if (logic.isOwnerOrAdmin)
                          ImageRes.editName.toImage
                            ..width = 12.w
                            ..height = 12.h,
                      ],
                    ),
                  ),
                  4.verticalSpace,
                  logic.groupInfo.value.groupID.toText
                    ..style = Styles.ts_8E9AB0_14sp
                    ..onTap = logic.copyGroupID,
                ],
              ),
            ),
          ],
        ),
      );

  Widget _buildMemberView() => Container(
        decoration: BoxDecoration(
          color: Styles.c_FFFFFF,
          borderRadius: BorderRadius.circular(6.r),
        ),
        margin: EdgeInsets.symmetric(horizontal: 10.w),
        child: Column(
          children: [
            GridView.builder(
              physics: const NeverScrollableScrollPhysics(),
              itemCount: logic.length(),
              shrinkWrap: true,
              padding: EdgeInsets.symmetric(horizontal: 2.w, vertical: 8.h),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 5,
                crossAxisSpacing: 3.w,
                mainAxisSpacing: 2.h,
                childAspectRatio: 68.w / 78.h,
              ),
              itemBuilder: (BuildContext context, int index) {
                return logic.itemBuilder(
                  index: index,
                  builder: (info) => Column(
                    children: [
                      SizedBox(
                        width: 58.w,
                        child: Stack(
                          alignment: Alignment.center,
                          children: [
                            AvatarView(
                              width: 48.w,
                              height: 48.h,
                              url: info.faceURL,
                              text: info.nickname,
                              textStyle: Styles.ts_FFFFFF_14sp,
                              onTap: () => logic.viewMemberInfo(info),
                            ),
                            if (logic.groupInfo.value.ownerUserID == info.userID)
                              Positioned(
                                bottom: 0.h,
                                child: Container(
                                  width: 52.h,
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                    color: Styles.c_E8EAEF,
                                    borderRadius: BorderRadius.circular(6.r),
                                  ),
                                  child: StrRes.groupOwner.toText
                                    ..style = Styles.ts_8E9AB0_10sp
                                    ..maxLines = 1
                                    ..overflow = TextOverflow.ellipsis,
                                ),
                              )
                          ],
                        ),
                      ),
                      2.verticalSpace,
                      (IMUtils.getGroupMemberShowName(info) ?? '').toText
                        ..style = Styles.ts_8E9AB0_10sp
                        ..maxLines = 1
                        ..overflow = TextOverflow.ellipsis,
                    ],
                  ),
                  addButton: () => logic.isGroupFeeEnabled
                      ? const SizedBox.shrink()
                      : GestureDetector(
                          onTap: logic.addMember,
                          child: Column(
                            children: [
                              ImageRes.addMember.toImage
                                ..width = 48.w
                                ..height = 48.h,
                              StrRes.addMember.toText..style = Styles.ts_8E9AB0_10sp,
                            ],
                          ),
                        ),
                  showMoreButton: () => GestureDetector(
                    onTap: logic.toggleShowAllMembers,
                    child: Column(
                      children: [
                        Container(
                          width: 48.w,
                          height: 48.h,
                          decoration: BoxDecoration(
                            color: Styles.c_F0F2F6,
                            borderRadius: BorderRadius.circular(6.r),
                          ),
                          child: Icon(
                            logic.showAllMembers.value ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                            size: 24.w,
                            color: Styles.c_8E9AB0,
                          ),
                        ),
                        2.verticalSpace,
                        (logic.showAllMembers.value ? '收起' : '更多').toText
                          ..style = Styles.ts_8E9AB0_10sp,
                      ],
                    ),
                  ),
                  delButton: () => GestureDetector(
                    onTap: logic.removeMember,
                    child: Column(
                      children: [
                        ImageRes.delMember.toImage
                          ..width = 48.w
                          ..height = 48.h,
                        StrRes.delMember.toText..style = Styles.ts_8E9AB0_10sp,
                      ],
                    ),
                  ),
                );
              },
            ),
            Container(
              color: Styles.c_E8EAEF,
              height: 1,
              margin: EdgeInsets.symmetric(horizontal: 10.w),
            ),
            // GestureDetector(
            //   behavior: HitTestBehavior.translucent,
            //   onTap: logic.viewGroupMembers,
            //   child: Container(
            //     padding: EdgeInsets.only(left: 12.w, right: 16.w),
            //     height: 46.h,
            //     child: Row(
            //       children: [
            //         sprintf(StrRes.viewAllGroupMembers, [logic.groupInfo.value.memberCount]).toText
            //           ..style = Styles.ts_0C1C33_17sp,
            //         const Spacer(),
            //         ImageRes.rightArrow.toImage
            //           ..width = 24.w
            //           ..height = 24.h,
            //       ],
            //     ),
            //   ),
            // ),
          ],
        ),
      );

  Widget _buildItemView({
    required String text,
    TextStyle? textStyle,
    String? value,
    Widget? customRight, // 新增自定义右侧内容参数
    bool switchOn = false,
    bool isTopRadius = false,
    bool isBottomRadius = false,
    bool showRightArrow = false,
    bool showSwitchButton = false,
    ValueChanged<bool>? onChanged,
    Function()? onTap,
  }) =>
      GestureDetector(
        onTap: onTap,
        behavior: HitTestBehavior.translucent,
        child: Container(
          height: 46.h,
          margin: EdgeInsets.symmetric(horizontal: 10.w),
          padding: EdgeInsets.symmetric(horizontal: 16.w),
          decoration: BoxDecoration(
            color: Styles.c_FFFFFF,
            borderRadius: BorderRadius.only(
              topRight: Radius.circular(isTopRadius ? 6.r : 0),
              topLeft: Radius.circular(isTopRadius ? 6.r : 0),
              bottomLeft: Radius.circular(isBottomRadius ? 6.r : 0),
              bottomRight: Radius.circular(isBottomRadius ? 6.r : 0),
            ),
          ),
          child: Row(
            children: [
              // 左侧标题文本
              text.toText
                ..style = textStyle ?? Styles.ts_0C1C33_17sp
                ..maxLines = 1,
              
              // 中间可扩展区域
              Expanded(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    // 自定义右侧内容
                    if (customRight != null) 
                      customRight,
                    
                    // value 文本
                    if (customRight == null && null != value)
                      Flexible(
                        child: value.toText
                          ..style = Styles.ts_8E9AB0_14sp
                          ..maxLines = 1
                          ..overflow = TextOverflow.ellipsis
                          ..textAlign = TextAlign.end,
                      ),
                    
                    // 开关按钮
                    if (showSwitchButton) ...[
                      if (value != null) 8.horizontalSpace,
                      CupertinoSwitch(
                        value: switchOn,
                        activeColor: Styles.c_FFB300,
                        onChanged: onChanged,
                      ),
                    ],
                  ],
                ),
              ),
              
              // 固定位置的箭头 - 始终在最右侧
              if (showRightArrow) ...[
                8.horizontalSpace,
                SizedBox(
                  width: 24.w,
                  height: 24.h,
                  child: ImageRes.rightArrow.toImage
                    ..width = 24.w
                    ..height = 24.h,
                ),
              ],
            ],
          ),
        ),
      );
}